<?php 
#--------------function-------------------------------
	function execute($query)
	{
		global $e_conn;
		return mysqli_query($e_conn, $query);
	}

	function first_row($query)
	{
		global $e_conn;
		$result = mysqli_query($e_conn, $query);
		if ($result)
			return mysqli_fetch_array($result);
		else
			return false;
	}

	function next_row($result)
	{
		return mysqli_fetch_array($result);	
	}

	function get_user_role()
	{
		global $wpdb;
		$uid = wp_get_current_user()->ID;
		$role = $wpdb->get_var("SELECT meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'wp_capabilities' AND user_id = {$uid}");
		  if(!$role) return 'non-user';
		$rarr = unserialize($role);
		$roles = is_array($rarr) ? array_keys($rarr) : array('non-user');
		return $roles[0];
	}

	function is_full_slot($event_id){
		global $EVENT;
		global $EVENT_JOIN;
		$query = "SELECT (`slot`=0) OR 
						 (`slot` > (SELECT COUNT(*) FROM `$EVENT_JOIN` WHERE `event_id` = '$event_id') ) AS isEmpty
				  FROM `$EVENT` WHERE `event_id` = '$event_id'";
		$result=first_row($query);
		return !$result['isEmpty'];
	}

	function remain_slot($event_id){
		global $EVENT;
		global $EVENT_JOIN;
		$query = "SELECT `slot` FROM `$EVENT` WHERE `event_id` = '$event_id'";
		$result = first_row($query);
		$slot = $result['slot'];
		if ($slot == 0)
		{
			return "no-limit";
		}

		$query = "SELECT COUNT(*) AS COUNT FROM `$EVENT_JOIN` WHERE `event_id` = '$event_id'";
		$result = first_row($query);
		$count = $result['COUNT'];

		return intval($slot) - intval($count);
	}

	function event_count(){
		global $EVENT;
		$query = "SELECT COUNT(*) AS COUNT FROM `$EVENT`";
		$result = first_row($query);
		$count = $result['COUNT'];
		return intval($count);
	}

	# return 1 when time1 > time2
	# return 2 when time1 < time2
	function compare_date($time1, $time2)
	{
		$t1 = explode("-", $time1);
		$d1 = intval($t1[0]);
		$M1 = intval($t1[1]);
		$y1 = intval($t1[2]);
		$h1 = intval($t1[3]);
		$m1 = intval($t1[4]);

		$t2 = explode("-", $time2);
		$d2 = intval($t2[0]);
		$M2 = intval($t2[1]);
		$y2 = intval($t2[2]);
		$h2 = intval($t2[3]);
		$m2 = intval($t2[4]);

		if ($y2 > $y1) {
			return 2;
		}
		if ($y2 < $y1) {
			return 1;
		}
		if ($M2 > $M1) {
			return 2;
		}
		if ($M2 < $M1) {
			return 1;
		}
		if ($d2 > $d1) {
			return 2;
		}
		if ($d2 < $d1) {
			return 1;
		}
		if ($h2 > $h1) {
			return 2;
		}
		if ($h2 < $h1) {
			return 1;
		}
		if ($m2 > $m1) {
			return 2;
		}
		if ($m2 < $m1) {
			return 1;
		}
		return 0;
	}

	//get the attendee list for admin
	function get_attendees_admin($event_id)
	{
		$query = "SELECT wp_users.`ID` AS `id`, `is_confirm`, wp_users.`phone_number`,
						wp_users.`user_facebook`, wp_users.`user_email`,
						DATE_FORMAT(`register_time`,'%H:%i:%S  %d-%m-%Y') AS `register_time`
				  FROM  `event_join` 
				  INNER JOIN  `wp_users` ON event_join.`user_login` = wp_users.`user_login`
				  WHERE `event_id` = '$event_id'
				  ORDER BY `is_confirm` DESC, `register_time`";
		return execute($query);
	}

	//get the attendee list for user
	function get_attendees_user($event_id)
	{
		$query = "SELECT wp_users.`ID` AS `id`, `is_confirm`
				  FROM  `event_join` 
				  INNER JOIN  `wp_users` ON event_join.`user_login` = wp_users.`user_login`
				  WHERE `event_id` = '$event_id'
				  ORDER BY `is_confirm` DESC, `register_time`";
		return execute($query);
	}

	//get the answer of user in a event
	function get_answers_by_user_and_event($user_login, $event_id)
	{
		$query = "SELECT poll_answer 
				  FROM event_poll_result INNER JOIN event_poll_answer ON event_poll_result.`answer_id` = event_poll_answer.`answer_id`
				  WHERE `user_login` = '$user_login' 
					  	AND event_poll_result.`answer_id` 
					  		IN (SELECT `answer_id` FROM `event_poll_answer` WHERE `event_id` = '$event_id')";
		$result = execute($query);
		$answer = "";
		while ($row = next_row($result)) {
			$ans = $row['poll_answer'];
			$answer .= "&#8226" . $ans . "<br>";
		}
		return $answer;
	}

	//poll
	function add_question($event_id, $poll_question, $poll_type, $poll_answer)
	{
		$sql = "INSERT INTO `event_poll`(`event_id`, `poll_question`, `poll_type`) 
				VALUES ($event_id, '$poll_question', '$poll_type')";

		if(!execute($sql)){
			return false;
		}

		$list = explode("\n", $poll_answer);
		foreach ($list as $key => $row) {
			$ans = $row;
			$sql = "INSERT INTO `event_poll_answer`(`poll_answer`, `event_id`) 
					VALUES ('$ans', $event_id)";
			execute($sql);
		}
		return true;
	}

	function get_poll($event_id)
	{
		$sql = "SELECT * FROM `event_poll` WHERE `event_id` = $event_id";
		return first_row($sql);
	}

	function get_answer($event_id)
	{
		$sql = "SELECT * FROM `event_poll_answer` WHERE `event_id` = $event_id";
		return execute($sql);
	}
 ?>